package com.winchain.chainbill.chain.callback.service.impl;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.acct.transaction.service.AcctTxRecService;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.persistence.transaction.Transaction;
import com.shell.bee.persistence.transaction.TransactionUtil;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.chain.callback.service.RecAdjustmentService;
import com.winchain.chainbill.trade.service.TradeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("recAdjustmentService")
public class RecAdjustmentServiceImpl implements RecAdjustmentService {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private TradeService tradeService;
	private AcctTxRecService AcctTxRecService;

	public boolean adjustRec(AcctTxRec AcctTxRec) {
		final AcctTxRec tran = getADAcctTxRec(AcctTxRec);
		this.AcctTxRecService.save(tran);
		try {
			TransactionUtil.execute(new Transaction() {
				public void doInTransaction() {
					RecAdjustmentServiceImpl.this.tradeService.fire(tran);
				}
			}, new int[0]);
		} catch (Exception e) {
			e.printStackTrace();
			this.logger.error("recAdjustment trade error");
		}
		return false;
	}

	private AcctTxRec getADAcctTxRec(AcctTxRec AcctTxRec) {
		AcctTxRec txRec = new AcctTxRec();
		BeanUtils.copyProperties(AcctTxRec, txRec);

		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(SystemUtil.getUUID());
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs("������������");
		txRec.setTxType("rec_ad");
		txRec.setTxSts("I");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setTxOperator("System");

		txRec.setTxAcctNo(AcctTxRec.getCpAcctNo());
		txRec.setTxAcctNm(AcctTxRec.getCpAcctNm());
		txRec.setTxBankNm(AcctTxRec.getCpBankNm());

		txRec.setCpAcctNo(AcctTxRec.getTxAcctNo());
		txRec.setCpAcctNm(AcctTxRec.getTxAcctNm());
		txRec.setCpBankNm(AcctTxRec.getTxBankNm());

		return txRec;
	}
}
